# -*- coding: utf-8 -*-
import gdata.calendar.service
import gdata.service
import gdata.calendar
import atom.data
import gdata.data
import gdata.calendar.client
import gdata.calendar.data
import time

class Client:
    def __init__(self, login, password, app_name, utc_offset):
        self.__client = self.login(login, password, app_name)
        self.__TRIES = 5
        self.__MAX_DELETE_ITERATIONS = 4
        self.__UTC_OFFSET = utc_offset

    def login(self, login, password, app_name):
        client = gdata.calendar.client.CalendarClient(source='Google-Calendar_Python_Sample-1.0')
        # client = gdata.calendar.service.CalendarService()
        client.source = app_name
        ok = False
        i = 0
        while ok != True:
            try:
                client.ClientLogin(login, password, 'cl')
                ok = True
            except gdata.service.CaptchaRequired:
                i+=1
            except gdata.service.BadAuthentication:
                i+=1
            except gdata.service.Error:
                i+=1
        return client

    def create_event(self):
        event = gdata.calendar.data.CalendarEventEntry()
        event.title = atom.data.Title(text=u"Тестовое событие")
        event.content = atom.data.Content(text=u"Контент тестового события")
        event.where.append(gdata.data.Where(value=u"Где произойдет"))
        start_time = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.gmtime())
        end_time = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.gmtime(time.time() + 3600))
        event.when.append(gdata.data.When(start=start_time, end=end_time))
        return event

    def get_calendar_uri(self, calendar_id):
        return "http://www.google.com/calendar/feeds/" + calendar_id + "/private/full/"

    def insert_event(self, calendar_id, event):
        self.__client.InsertEvent(event, self.get_calendar_uri(calendar_id))

utc_offset = 4
client = Client('help.vladimir@gmail.com', 'zaq1xsw2123', 'test-python-app', 4)
event = client.create_event()
client.insert_event("qik6u8q6od7fhvalrk6gpmhivo@group.calendar.google.com", event)